package com.example.myapplication.fragments;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.R;
import com.example.myapplication.bean.ClassMate;
import com.example.myapplication.utli.UtilsEnum;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicReference;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link updFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class updFragment extends Fragment {

    int id , type ;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public updFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment updFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static updFragment newInstance(String param1, String param2) {
        updFragment fragment = new updFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            this.id = getArguments().getInt("id");
            this.type = getArguments().getInt("type");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_upd, container, false);
        EditText edContent = view.findViewById(R.id.et_content_upd);
        ImageView imgBack = view.findViewById(R.id.img_back_upd);
        Button btnSave = view.findViewById(R.id.btn_save_upd);
        Log.e("TAG", "onCreateView: " + id + "------" +type);
        RequestQueue queue = Volley.newRequestQueue(getContext());
        AtomicReference<ClassMate.DataDTO> data  = new AtomicReference<>(new ClassMate.DataDTO());
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, UtilsEnum.URL.url + "classmate/detail/"+UtilsEnum.ID ,new JSONObject(),
                res -> {
                    res.toString();
                    Gson gson = new Gson();
                    ClassMate classMate = gson.fromJson(res.toString(), ClassMate.class);
                    data.set(classMate.getData());
                    switch (type){
                        case 0:
                            edContent.setText(data.get().getName());
                            break;
                        case 1:
                            edContent.setText(data.get().getDepartment());
                            break;
                        case 2:
                            edContent.setText(data.get().getMajor());
                            break;
                        case 3:
                            edContent.setText(data.get().getTelephone());
                            break;
                    }
                },(error->{
            Log.e("TAG", "onCreateView: " + error );
        }));
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (type){
                    case 0:
                        data.get().setName(edContent.getText()+ "");
                        break;
                    case 1:
                        data.get().setDepartment(edContent.getText()+ "");
                        break;
                    case 2:
                        data.get().setMajor(edContent.getText()+ "");
                        break;
                    case 3:
                        data.get().setTelephone(edContent.getText()+ "");
                        break;
                }
                JsonObjectRequest request1 = new JsonObjectRequest(Request.Method.POST,UtilsEnum.URL.url + "/user/update?id="+UtilsEnum.ID+"&name="+data.get().getName()+"&password="+data.get().getPassword()+"&email="+data.get().getEmail()+"&telephone="+data.get().getTelephone()+"&department="+data.get().getDepartment()+"&major="+data.get().getMajor()+"&role="+data.get().getRole()
                        ,new JSONObject(),(res->{
                            Context context;
                            CharSequence text;
                            Toast.makeText(getContext(), "保存成功",Toast.LENGTH_SHORT).show();
                }),(error->{
                    Log.e("TAG", "onClick: " + error );
                }));
                queue.add(request1);
            }
        });
        queue.add(request);

        return view;
    }
}